package com.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mp.core.utils.Utils;
import com.mp.dao.AmountRecordMapper;
import com.mp.dao.UserInfoMapper;
import com.mp.pojo.AmountRecord;
import com.mp.pojo.Query.AmountQuery;
import com.mp.pojo.Query.UserinfoSearchQuery;
import com.mp.pojo.UserInfo;
import com.mp.pojo.VO.UserInfoSearchVO;
import com.mp.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author jack
 * @data 2019-08-09 14:34
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AmountRecordMapper amountRecordMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public String updateAmount(AmountQuery amountQuery) {

        AmountRecord amountRecord = new AmountRecord();
        BeanUtils.copyProperties(amountQuery, amountRecord);
        /**
         * 状态值错误  1充值，2扣费
         *
         */
        // 查找该用户
        UserInfo selectUser = userInfoMapper.selectById(amountRecord.getUserId());
        if (selectUser == null) {
            return "该用户不存在";
        }
        // 修改用户余额,添加对应的金额流向记录
        // 当status 为扣费时需要将费用设为负数
        if (amountRecord.getStatus().equals(2)) {
            amountRecord.setAmount(new BigDecimal(0).subtract(amountRecord.getAmount()));
        }
        new LambdaUpdateChainWrapper<>(userInfoMapper).eq(UserInfo::getId, amountRecord.getUserId())
                .setSql("balance = balance +" + amountRecord.getAmount())
                .update();


        amountRecord.setAmountBefore(selectUser.getBalance());
        amountRecord.setAmountNow(selectUser.getBalance().add(amountRecord.getAmount()));
        amountRecordMapper.insert(amountRecord);
        return null;
    }


    /**
     * 搜索用户
     * @param userSearchQuery
     * @return
     */
    @Override
    public IPage<UserInfoSearchVO> getPageBySeach(UserinfoSearchQuery userSearchQuery) {

        Page<UserInfo> page = new Page<>(userSearchQuery.getCurrent(), userSearchQuery.getSize());
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(userSearchQuery.getUserName()),"u.name", userSearchQuery.getUserName())
                .between(!Utils.isNull(new Object[]{userSearchQuery.getStartTimeStr(), userSearchQuery.getEndTimeStr()}),
                        "u.create_time", userSearchQuery.getStartTimeStr(), userSearchQuery.getEndTimeStr())
                .select("u.id","u.name");

        IPage<UserInfoSearchVO> iPage = userInfoMapper.getPageBySeach(page, queryWrapper);
        System.out.println(iPage.getTotal());
        return iPage;
    }
}
